<?php

#require_once 'Auth/OpenID/Consumer.php';
#require_once "Auth/OpenID/FileStore.php";
#require_once "Auth/OpenID/SReg.php";
#require_once 'Auth/Yadis/Yadis.php';
require 'openid.php';
require_once '../RestParser.php';
session_start();

/**
 * Base class for parsing out a REST rest request.
 */
class LoginRestParser extends RestParser {

	private $store = null;
	private $consumer = null;
	private $openid = null;
	
    /**
     * Associative array of valid REST operations.
     */
    protected $VALID_REST_ACTIONS = array(
        'login',
        'logout', 
    );

    
    public function __construct($args) {
    	parent::__construct($args);
    	$this->openid = new LightOpenID('192.168.2.2');
    }
    
    
    /**
     * Returns an array of the valid REST actions.
     * 
     * @return array Valid REST actions.
     */
    public function getValidRestActions() {
        return $this->VALID_REST_ACTIONS;
    }

    
    public function handleAction($action, $restParms){
    	switch(strtolower($action)){
    		case 'login':
    			$this->login();
    			break;
    		case 'logout':
    			$this->logout();
    			break;
    	}
    }
        
    private function getReturnTo(){
    	return 'www.google.com';
    }

    private function getTrustRoot(){
    	return 'www.google.com';
    }

    public function check(){
    	if (!isset($_SESSION['identity'])){
    		$this->login();
    	}else{
    		$this->openid->validate();
    	}
    }
    
    private function login(){
    	if(!$this->openid->mode) {
   			$this->openid->identity = 'https://www.google.com/accounts/o8/id';
   			header('Location: ' . $this->openid->authUrl());
  		} elseif($this->openid->mode == 'cancel') {
  			// not implemented yet	
    	} else {
    		preg_match('(id=(.*))', $_GET['openid_claimed_id'], $mat);
    		$_SESSION['identity'] = $mat[1];
    	}   	
    }
    
    private function logout(){
    	unset($_SESSION['identity']);
    	//$this->openid
    	print "unsetting id";
    }
    
    public function toString(){
    	return $rand;
    }
    
    // login is the auth class so we obviously don't need to check for auth 
    public function requiresAuth(){
    	return false;
    }
    
}

?>
